package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.common.utils.R;



/**
 * 商品三级分类
 *
 * @author xufb
 * @email 2635612275@qq.com
 * @date 2023-04-11 10:28:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("product/category")
public class CategoryController {

   private final CategoryService categoryService;

    /**
     * 查出所有分类以及子类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list(){
        List<CategoryEntity> listWithTree = categoryService.listWithTree();

        return R.ok().put("data", listWithTree);
    }


    /**
     * 获取信息，用于查询、回显.....     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok("保存成功!");
    }


    /**
     * 修改
     * @param category 菜单Json数据
     * @return R
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateCascade(category);
        return R.ok("修改成功!");
    }

    /**
     * 更新拖拽功能产生的层级修改
     * @param categoryDropList 拖拽产生变动的的对象数组
     * @return R
     */
    @RequestMapping("/update/sort")
    public R updateByDropSort(@RequestBody CategoryEntity[] categoryDropList){
        categoryService.updateBatchById(Arrays.asList(categoryDropList));
        return R.ok("拖拽修改成功!");
    }

    /**
     * 删除
     * 只要看到  @RequestBody 就是post请求
     * @param catIds 菜单id
     * @return R
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok("删除成功!");
    }

}
